package com.magic.gre.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.magic.gre.GREApp;
import com.magic.gre.R;
import com.magic.gre.entity.WordsDetailsBean;
import com.noname.lib_base_java.adapter.CommonAdapter;
import com.noname.lib_base_java.adapter.ViewHolder;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Author: Lmoumou
 * Date: 2019-01-08 12:24
 * 文件名: WordsDetailsAdapter
 * 描述:
 */
public class WordsDetailsAdapter extends CommonAdapter<WordsDetailsBean> {

    private OnItemClickListener onItemClickListener;
    private Map<Integer, AnimationDrawable> aniMap;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public WordsDetailsAdapter(Context mContext, List<WordsDetailsBean> mData, Map<Integer, AnimationDrawable> aniMap) {
        super(mContext, mData, R.layout.item_words_details);
        this.aniMap = aniMap;
    }

    @Override
    protected void bindData(final ViewHolder holder, int position) {
        final WordsDetailsBean bean = mData.get(position);
        holder.setText(R.id.words_tv, bean.getName());
        TextView pronunciationTv = holder.getView(R.id.pronunciation_tv);
        pronunciationTv.setText(bean.getSymbol());
        Drawable[] compoundDrawables = pronunciationTv.getCompoundDrawables();
        final AnimationDrawable animation = (AnimationDrawable) compoundDrawables[2];
        animation.stop();
        animation.selectDrawable(0);
        aniMap.put(position, animation);
        if (onItemClickListener != null) {
            String dirPath = Environment.getExternalStorageDirectory() + File.separator + GREApp.getContext().getPackageName() + File.separator + "Download" + File.separator + bean.getThesaurusId();
            onItemClickListener.onVoice(animation, dirPath, position);
        }

        holder.getView(R.id.check_tv).setVisibility(bean.isShow() ? View.GONE : View.VISIBLE);

        RecyclerView recyclerView = holder.getView(R.id.mRecyclerView);
        recyclerView.setVisibility(bean.isShow() ? View.VISIBLE : View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        recyclerView.setAdapter(new MeanAdapter(mContext, bean.getMeanings()));
        holder.getView(R.id.check_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.getView(R.id.mRecyclerView).setVisibility(View.VISIBLE);
                bean.setShow(true);
                v.setVisibility(View.GONE);
            }
        });
        pronunciationTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    String dirPath = Environment.getExternalStorageDirectory() + File.separator + GREApp.getContext().getPackageName() + File.separator + "Download" + File.separator + bean.getThesaurusId();
                    onItemClickListener.onPlayVoice(animation, bean.getVoicePath(dirPath));
                }
//                animation.start();
            }
        });
    }

    public interface OnItemClickListener {

        void onVoice(AnimationDrawable animation, String voicePath, int position);

        void onPlayVoice(AnimationDrawable animation, String voicePath);
    }

}
